define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data("area",["90%","90%"]);
            $(".btn-qadd").data("area",["80%","80%"]);
            $(".btn-edit").data("area",["90%","90%"]);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'staff/staff/index' + location.search,
                    add_url: 'staff/staff/add',
                    qadd_url: 'staff/staff/qadd',
                    edit_url: 'staff/staff/edit',
                    test_url: 'staff/staff/detail',
                    del_url: 'staff/staff/del',
                    multi_url: 'staff/staff/multi',
                    import_url: 'staff/staff/import',
                    export_url: 'staff/staff/export',
                    table: 'staff',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                       // {field: 'id', title: __('Id')},
                        {field: 'staffcode', title: __('Staffcode'),operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'type', title: __('Type'), searchList: {"0":'正式工',"1":'退休返聘',"2":'实习生',"3":'派遣工'}, formatter: Table.api.formatter.normal},
                        {field: 'shop_id', title: __('所属部门'),visible: false,addClass:'selectpage',extend:'data-source="census/shop" data-primary-key="id" data-show-field="shopname"'},
                        {field: 'department.shopname', title: __('Department'),addClass:'selectpage',extend:'data-source="census/shop" data-primary-key="id" data-show-field="shopname"',operate:false},
                        {field: 'jobgrade.name', title: __('Jobgrade'), operate:false},
                        {field: 'rank.name', title: __('Rank'), operate:false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'staffrank.name', title: __('角色'),addClass:'selectpage',extend:'data-source="wanlshop/staff/rank" data-primary-key="id" data-show-field="name"',operate:false},
                        {field: 'rank_id', title: __('角色'),addClass:'selectpage',extend:'data-source="wanlshop/staff/rank" data-primary-key="id" data-show-field="name"',visible:false},
                        {field: 'signingtime', title: __('Signingtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'expirationtime', title: __('Expirationtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('离职'),"1":__('在职')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [{
                            name: "test",
                            text: "查看/编辑",//按钮名称
                            classname: 'btn btn-xs btn-success btn-magic ',
                            // classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                            icon: 'fa fa-magic',
                            url: 'staff/staff/detail',//指向控制器对应方法
                            confirm: '查看/编辑信息',
                            visible: function (row) {
                                //返回true时按钮显示,返回false隐藏
                                return true;
                            }
                        }], events: Table.api.events.operate, formatter: function(value,row,index){
                                var that = $.extend({},this);
                                var table = $(that.table).clone(true);

                                //隐藏编辑按钮
                                $(table).data("operate-edit",null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            // 初始化表格参数配置
            var staff_id = $('#he').val();
            var card_id = $('#card').val();
            var soid = $('#soid').val();
            var hid = $('#hid').val();
            Fast.config.openArea = ['90%','60%'];

            $(document).on("focus", ".staff-edit", function () {
                Fast.api.open('staff/staff/edit/ids/' + staff_id, "人事信息", {
                    shade:0.1,
                    area:['100%','100%'],
                });

               // Backend.api.open('staff/staff/edit/ids/' + staff_id, __('Detail'),{area: ['100%', '100%']});
            });
            require(['upload'], function(Upload){
                Upload.api.plupload();
            });

            $(document).on("focus", ".card-edit", function () {
                var url = "{:url('staff/card/edit/ids/')}"+card_id;
                console.log(url);
                Backend.api.open('staff/card/edit/ids/' + card_id, __('Detail'));
            });
            $(document).on("focus", ".social-edit", function () {
                Backend.api.open('staff/social/edit/ids/' + soid, __('Detail'));
            });
            $(document).on("focus", ".housing-edit", function () {
                Backend.api.open('staff/housing/edit/ids/' + hid, __('Detail'));
            });

            /** 员工调动 **/
             $(document).on("click", ".btn-transfer", function () {
                Backend.api.open('staff/staff/transfer/ids/' + staff_id, __('员工调动'),{area: ['50%', '50%']});
            });

            /** 办理离职 **/
             $(document).on("click", ".btn-resign", function () {
                Fast.api.ajax({
                    url: "staff/staff/resign/ids/"+staff_id,
                    data: {},
                    success: function (data) {
                        if (data.code == 1) {
                            Toastr.error('离职办理成功');
                            window.location.reload();
                        }else{
                            Toastr.error(ret.msg);
                        }
                    }
                })
            });

            Table.api.init();
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                extend: {
                    index_url: 'staff/staff/wages/ids'+ staff_id + location.search,
                        add_url: 'staff/salary/add?staff_id='+ staff_id,
                        edit_url: 'staff/salary/edit',
                        multi_url: 'staff/salary/multi',
                        import_url: 'staff/salary/import',
                        table: 'staff_salary',
                },
                url:'staff/staff/wages/ids/'+ staff_id + location.search,
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    var id = $('#he').html();
                    filter.staff_id= id;     //将透传的参数 Config.group_id，追加到搜索条件中
                    op.staff_id= "=";  //group_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;

                },
                sortName: 'id',
                toolbar: '#toolbar',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'salaryyearmonth', title: __('Salaryyearmonth'), operate: false, addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM"},
                        {field: 'basepay', title: __('Basepay'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'positionsalary', title: __('Positionsalary')},
                        {field: 'meritpay', title: __('Meritpay'), operate: 'LIKE'},
                        {field: 'unitvacation', title: __('Unitvacation')},
                        {field: 'othersubsidies', title: __('Othersubsidies'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'senioritysalary', title: __('Senioritysalary')},
                        // {field: 'vacationsalary', title: __('Vacationsalary'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'overtimepay', title: __('Overtimepay')},
                        {field: 'mealsupplements', title: __('Mealsupplements')},
                        {field: 'heatingsubsidy', title: __('Heatingsubsidy')},
                        {field: 'otherrewards', title: __('Otherrewards')},

                        {field: 'fixedsalary', title: __('Fixedsalary')},
                        {field: 'totalsubsidy', title: __('Totalsubsidy')},
                        {field: 'totalamount', title: __('Totalamount')},
                        {field: 'netsalary', title: __('Netsalary')},
                        {field: 'otherpayments', title: __('Otherpayments')},
                        {field: '0therdeductions', title: __('Otherdeductions')},
                        {field: 'elderlycare', title: __('Elderlycare')},
                        {field: 'medicalcare', title: __('Medicalcare')},
                        {field: 'unemployment', title: __('Unemployment')},
                        {field: 'accumulationfund', title: __('Accumulationfund')},
                        {field: 'personalincometax', title: __('Personalincometax')},
                        {field: 'servicecharge', title: __('Servicecharge')},
                        {field: 'unitpayssocialsecurity', title: __('Unitpayssocialsecurity')},
                        {field: 'unitpayshousingprovidentfund', title: __('Unitpayshousingprovidentfund')},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                    ],

                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            Table.api.init();
            var table1 = $("#table1");

            // 初始化表格
            table1.bootstrapTable({
                url: 'staff/achievements/index' + location.search,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar1',
                fixedColumns: true,
                extend: {
                    index_url: 'staff/achievements/index' + location.search,
                        add_url: 'staff/achievements/add?staff_id='+ staff_id,
                        edit_url: 'staff/achievements/add?staff_id='+ staff_id,
                        del_url: 'staff/achievements/del',
                        multi_url: 'staff/achievements/multi',
                        import_url: 'staff/achievements/import',
                        table: 'staff_achievements',
                },
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    var id = $('#he').html();
                    filter.staff_id= id;     //将透传的参数 Config.group_id，追加到搜索条件中
                    op.staff_id= "=";  //group_id的操作方法的为 找到相等的
                    params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                    params.op = JSON.stringify(op);
                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;

                },
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'staff_id', title: __('Staff_id')},
                        // {field: 'shop_id', title: __('Shop_id')},
                        {field: 'achievements_time', title: __('Achievements_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'jan', title: __('Jan'), operate:'BETWEEN'},
                        {field: 'feb', title: __('Feb'), operate:'BETWEEN'},
                        {field: 'mar', title: __('Mar'), operate:'BETWEEN'},
                        {field: 'apr', title: __('Apr'), operate:'BETWEEN'},
                        {field: 'may', title: __('May'), operate:'BETWEEN'},
                        {field: 'jun', title: __('Jun'), operate:'BETWEEN'},
                        {field: 'jul', title: __('Jul'), operate:'BETWEEN'},
                        {field: 'aug', title: __('Aug'), operate:'BETWEEN'},
                        {field: 'sep', title: __('Sep'), operate:'BETWEEN'},
                        {field: 'oct', title: __('Oct'), operate:'BETWEEN'},
                        {field: 'nov', title: __('Nov'), operate:'BETWEEN'},
                        {field: 'dec', title: __('Dec'), operate:'BETWEEN'},
                        {field: 'first_quarter', title: __('First_quarter'), operate:'BETWEEN'},
                        {field: 'second_quarter', title: __('Second_quarter'), operate:'BETWEEN'},
                        {field: 'third_quarter', title: __('Third_quarter'), operate:'BETWEEN'},
                        {field: 'fourth_quarter', title: __('Fourth_quarter'), operate:'BETWEEN'},
                        {field: 'annual', title: __('Annual'), operate:'BETWEEN'},
                        {field: 'staff.name', title: __('Staff.name'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            // 给上传按钮添加上传成功事件
            $("#faupload-portraitimage").data("upload-success", function (data) {
                var url = Backend.api.cdnurl(data.url);
                var id = $('#he').html();
                console.log(url);
                Fast.api.ajax({
                    url: "staff/staff/saveportraitimage",
                    data: {
                        id: id,
                        portraitimage: url,
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            window.location.reload();
                        }
                    }
                })
                Toastr.success("上传成功！");
            });
        },
        export: function () {
            Controller.api.bindevent();
        },
        import: function () {
            Controller.api.bindevent();
        },
        qadd: function () {
            console.log(222222)
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                // Toastr.success(ret.msg);
                parent.location.reload();
                setTimeout(function () {
                  //  console.log(324444);
                    location.reload();
                }, 1500);
            }, function (data, ret) {
                Toastr.error(ret.msg);
                return false;
            });
           // Controller.api.bindevent();
        },
        transfer: function () {
            Form.api.bindevent($("form[role=form]"), function (data, ret) {
                // Toastr.success(ret.msg);
                parent.location.reload();
                setTimeout(function () {
                  //  console.log(324444);
                    location.reload();
                }, 1500);
            }, function (data, ret) {
                Toastr.error(ret.msg);
                return false;
            });
           // Controller.api.bindevent();
        },
        test: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },

    };

    return Controller;
});
